//JavaScript代码区域
layui.use(['layer', 'form', 'laydate', 'table'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		laydate = layui.laydate,
		table = layui.table;
	//监听提交
	form.on('submit(formDemo)', function(data) {
		table.reload('idTest', {
		  where: data.field
		});
		return false;
	});
	//执行一个laydate实例
	laydate.render({
		elem: '#start',
		max: 0
	});
	laydate.render({
		elem: '#end',
		max: 0
	});
	//数据表格实例
	table.render({
		id: 'idTest',
		elem: '#demo',
		url: IP+'/api/blog-admin/article',
		height: 'full-180',
		method: 'get',
		page: true,
		even: true,
		cols: [[
		   {type:'checkbox'}
	      ,{field: 'id', title: 'ID', sort: true}
	      ,{field: 'title', title: '标题',width:200}
	      ,{field: 'status', title: '状态'} 
	      ,{field: 'istop', title: '是否顶置',width:90}
	      ,{field: 'isvisible', title: '是否可见',width:90}
	      ,{field: 'createtime', title: '创建时间', sort: true}
	      ,{field: 'updatetime', title: '修改时间', sort: true}
	      ,{fixed: 'right', width:200, title: '操作', align:'center', toolbar: '#barDemo'}
	    ]],
	    request: {
		  pageName: 'page'
		  ,limitName: 'pageSize'
		},              
	    response:{
	    	  statusName: 'code'
			  ,statusCode: 200
			  ,msgName: 'msg'
			  ,countName: 'count'
			  ,dataName: 'data'
	    },
	    done:function(res, curr, count){
			layer.closeAll('loading');
			//更新一些字典数据
			refreshTableDict();
	    }
	});
	//数据表格监听工具条(查看、编辑、删除按钮)
	table.on('tool(test)', function(obj){
	  var data = obj.data;
	  var layEvent = obj.event;
	  if(layEvent === 'detail'){
	  	ViewUserDetail(data.id);
	  } else if(layEvent === 'del'){
	      var ts=data.title;
	      var bids=[];
	      bids.push(data.id)
		  deleteByUids(ts,bids);
	  } else if(layEvent === 'edit'){
	    window.location="article_update.html?id="+data.id;	    
	  }
	});
	//复选框批量操作
	table.on('checkbox(test)', function(obj){
		var checkStatus = table.checkStatus('idTest');
		if(checkStatus.data.length>0){
			$('#deleteall').show()
		}else{
			$('#deleteall').hide()
		}
	});
	//监听排序
	table.on('sort(test)', function(obj){
	  refreshTableDict();
	});
	$(function() {
		//批量删除按钮
		$('#deleteall').click(function() {
		var checkStatus = table.checkStatus('idTest');
			if(checkStatus.data.length > 0) {
				var list = checkStatus.data;
				var bids=[];
				var ts="";
				$.each(list, function(idx,obj) {
					bids.push(obj.id)
					ts+=" "+obj.title
				});
				deleteByUids(ts,bids);
			}
		})
		//添加按钮事件
		$('#adduser').on('click',function(){
			$('#article_add',top.document).trigger('click')
		})
	})
	
	form.on('switch(istop)', function(data){
		var id=data.elem.getAttribute("id");
		var data=JSON.stringify({
			id:id,
			istop:data.elem.checked?'1':'0'
		})
	  updateByCheckBox(data)
	}); 
	form.on('switch(isvisible)', function(data){
	  var id=data.elem.getAttribute("id");
		var data=JSON.stringify({
			id:id,
			isvisible:data.elem.checked?'1':'0'
		})
	  updateByCheckBox(data)
	});  
	//修改
	function updateByCheckBox(data){
		$.ajax({
			url: IP + '/api/blog-admin/article/switch',
			type: "PUT",
			data: data,
			success: function(result, status, xhr) {
				layer.closeAll('loading');
				layer.msg(result.msg, {
					icon: 1,
					time: 1000
				});
			}
		});
	}
	//单、批量删除
	function deleteByUids(ts,bids){
		layer.confirm('真的删除'+ts+"吗？",{icon:3}, function(index) {
			layer.close(index);
			$.ajax({
				url: IP + '/api/blog-admin/article',
				type: "DELETE",
				data: JSON.stringify(bids),
				success: function(result, status, xhr) {
					layer.closeAll('loading');
					layer.msg(result.msg, {
						icon: 1,
						time: 1000
					},function(){
						table.reload('idTest', {});
					});
				}
			});
		});
	}
	//更新表格字典信息
	function refreshTableDict(){
		var table_datas=$('#demo').siblings('.layui-form').children('.layui-table-box')
			.children('.layui-table-body').children('.layui-table').children('tbody').children('tr');
		$.each(table_datas, function(idx,obj) {
			var bid=$(this).children("td[data-field='id']").children('div').html();
			// 状态 1:审核通过，0:不通过
			var status=$(this).children("td[data-field='status']").children('div');
			if(status.html()==1){
				status.html("审核通过");
			}else if(status.html()==0){
				status.html("等待审核");
			}
			// 是否顶置 1:顶置，0:默认不顶置
			var istop=$(this).children("td[data-field='istop']").children('div');
			var top=istop.html()==1?"checked":"";
			istop.html("<input id='"+bid+"' lay-filter='istop' type='checkbox' name='istop' lay-skin='switch' lay-text='是|否' "+top+">");
			// 是否可见 1:显示，0:默认不显示，需要手动设置显示isvisible
			var isvisible=$(this).children("td[data-field='isvisible']").children('div');
			var vis=isvisible.html()==1?"checked":"";
			isvisible.html("<input id='"+bid+"' lay-filter='isvisible' type='checkbox' name='istop' lay-skin='switch' lay-text='是|否' "+vis+">");
			form.render('checkbox');
		});
	}
	//查询信息
	function ViewUserDetail(uid){
		layer.open({
		  type: 2,
		  skin:'layui-layer-molv',
		  area: ['600px', '340px'],
		  maxmin: true,
		  content: 'admin_query.html?uid='+uid
		});
	}
});